#include "YGPlatform.h"

#if (YG_OS_TYPE == YG_OS_TYPE_ANDROID)

#include "YGInputAndroid.h"

namespace YoghurtGum
{

	KeyboardAndroid::KeyboardAndroid()
	{
		
	}

	KeyboardAndroid::~KeyboardAndroid()
	{

	}

	void KeyboardAndroid::Init()
	{
		Log::GetSingleton().Print("Keyboard::Init");

		Log::GetSingleton().Print("Keyboard size: %i", sizeof(KeyboardAndroid));

		//m_KeysPressed = new bool[256];
		//m_KeysReleased = new bool[256];

		for (int i = 0; i < 256; i++) 
		{ 
			m_KeysPressed[i] = false; 
			m_KeysReleased[i] = false;
			m_KeysChanged[i] = false;
		}
	}

	void KeyboardAndroid::Clear()
	{
		//Log::GetSingleton().Print("Keyboard::Clear");

		for (int i = 0; i < 256; i++) 
		{ 
			m_KeysReleased[i] = false; 
		}
	}

	void KeyboardAndroid::Update()
	{
		for (int i = 0; i < 256; i++) 
		{ 
			if (m_KeysChanged[i]) 
			{ 
				Log::GetSingleton().Print(
					"Key changed: (Press: %s, Release: %s)", 
					m_KeysPressed[i] ? "TRUE" : "FALSE",
					m_KeysReleased[i] ? "TRUE" : "FALSE"
				);

				m_KeysPressed[i] = false; 
			}
			m_KeysChanged[i] = false;
		}
	}

	bool KeyboardAndroid::Pressed(Key a_Key)
	{
		return m_KeysPressed[a_Key];
	}

	bool KeyboardAndroid::Released(Key a_Key)
	{
		return m_KeysReleased[a_Key];
	}

	void KeyboardAndroid::InjectPress(Key a_Key)
	{
		//Log::GetSingleton().Print("InjectKeyPress: %i", a_Key);
		m_KeysPressed[a_Key] = true;
	}

	void KeyboardAndroid::InjectRelease(Key a_Key)
	{
		//Log::GetSingleton().Print("InjectKeyRelease: %i", a_Key);
		m_KeysChanged[a_Key] = true;
		m_KeysReleased[a_Key] = true;
	}


	// ================================
	// Touchscreen
	// ================================
	
	void TouchScreenAndroid::Init()
	{
		m_Device = &Device::Get();
		m_DeviceX = 1.f / (float)m_Device->GetWidth();
		m_DeviceY = 1.f / (float)m_Device->GetHeight();

		m_Pressed = m_Released = false;
	}

	void TouchScreenAndroid::Clear()
	{
		if (m_Clear)
		{
			m_Pressed = false;
			m_Released = true;
		}
	}

	void TouchScreenAndroid::Resize(int a_Width, int a_Height)
	{
		m_DeviceX = 1.f / (float)a_Width;
		m_DeviceY = 1.f / (float)a_Height;
	}

	void TouchScreenAndroid::InjectPress(float a_X, float a_Y)
	{
		m_X = a_X * m_DeviceX;
		m_Y = a_Y * m_DeviceY;
		m_ScreenX = (int)a_X;
		m_ScreenY = (int)a_Y;

		m_Pressed = true;
	}

	void TouchScreenAndroid::InjectRelease(float a_X, float a_Y)
	{
		m_Clear = true;
	}

	Vec2 TouchScreenAndroid::GetPos()
	{
		return Vec2(m_X, m_Y);
	}

	float TouchScreenAndroid::GetX()
	{
		return m_X;
	}

	float TouchScreenAndroid::GetY()
	{
		return m_Y;
	}

	int TouchScreenAndroid::GetScreenX()
	{
		return m_ScreenX;
	}

	int TouchScreenAndroid::GetScreenY()
	{
		return m_ScreenY;
	}

	bool TouchScreenAndroid::Released()
	{
		return m_Released;
	}

	bool TouchScreenAndroid::Pressed()
	{
		return m_Pressed;
	}

	TouchScreenAndroid::TouchScreenAndroid()
	{

	}

	TouchScreenAndroid::~TouchScreenAndroid()
	{

	}

}

#endif